package com.maya.medicalwastes.util;

import android.text.Editable;


import java.text.DecimalFormat;
import java.util.Date;

/**
 * @Creator cfy
 * @date 15/11/16 11:20
 * @Description <p/>
 * 格式校验工具类
 * ----------------------
 */
public class FormatUtils {
    /**
     * 判断字符串是否为Email *
     */
    public static boolean isEmail(String email) {
        if (email.matches("\\w+([-+.']\\w+)*@\\w+\\.\\w+([-.]\\w+)*")
                && email.length() > 0)
            return true;
        return false;
    }

    /**
     * 判断字符串是否为mobile *
     */
    public static boolean isMobilePhone(String mobile) {
        if (mobile.matches("1[0-9]{10}"))
            return true;
        return false;
    }

    /**
     * 判断字符串是否为数数字 *
     */
    public static boolean isNumber(String str) {
        if (str.matches("[0-9]*"))
            return true;
        return false;
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatStr if formatStr!=null
     * return “” if formatStr==null
     */
    public static String getObject(String formatStr) {
        return getObject(formatStr, "");
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatStr if formatStr!=null
     * return defaultStr if formatStr==null
     */
    public static String getObject(String formatStr, String defaultStr) {
        if (null == formatStr)
            return defaultStr;
        return formatStr;
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatInt if formatInt!=null
     * return 0 if formatInt==null
     */
    public static Integer getObject(Integer formatInt) {
        return getObject(formatInt, 0);
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatInt if formatInt!=null
     * return defaultInt if formatInt==null
     */
    public static Integer getObject(Integer formatInt, Integer defaultInt) {
        if (null == formatInt)
            return defaultInt;
        return formatInt;
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatDouble if formatDouble!=null
     * return 0 if formatDouble==null
     */
    public static Double getObject(Double formatInt) {
        return getObject(formatInt, 0.0);
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatDouble if formatDouble!=null
     * return defaultDouble if formatDouble==null
     */
    public static Double getObject(Double formatInt, Double defaultInt) {
        if (null == formatInt)
            return defaultInt;
        return formatInt;
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatBoolean if formatBoolean!=null
     * return false if formatBoolean==null
     */
    public static Boolean getObject(Boolean formatBoolean) {
        return getObject(formatBoolean, false);
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatBoolean if formatBoolean!=null
     * return defaultBoolean if formatBoolean==null
     */
    public static Boolean getObject(Boolean formatBoolean, Boolean defaultBoolean) {
        if (null == formatBoolean)
            return defaultBoolean;
        return formatBoolean;
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatLong if formatLong!=null
     * return 0l if formatLong==null
     */
    public static Long getObject(Long formatLong) {
        return getObject(formatLong, 0l);
    }

    /**
     * @author chengfy
     * created at 15/11/16 13:00
     * return formatLong if formatLong!=null
     * return defaultLong if formatLong==null
     */
    public static Long getObject(Long formatLong, Long defaultLong) {
        if (null == formatLong)
            return defaultLong;
        return formatLong;
    }


    /**
     * @author chengfy
     * created at 15/12/16 16:06
     * @description 限制小数点后的位数
     */
    public static void decimalDigits(Editable str) {
        decimalDigits(str, 2);
    }

    /**
     * @param DECIMAL_DIGITS 允许几位小数
     * @author chengfy
     * created at 15/12/16 16:06
     * @description 限制小数点后的位数
     */
    public static void decimalDigits(Editable str, int DECIMAL_DIGITS) {
        String temp = str.toString();
        int posDot = temp.indexOf(".");
        if (posDot <= 0) return;
        if (temp.length() - posDot - 1 > DECIMAL_DIGITS) {
            str.delete(posDot + DECIMAL_DIGITS + 1, posDot + DECIMAL_DIGITS + 2);
        }
    }

    public static String parseNumTwo(Double num){
        String numStr = roundByScale(num, 2);
/*        String afterData = numStr.substring(numStr.indexOf('.'));
        int afterLen = afterData.length();
        if(afterLen<2){
            for (int i = 0; i < 2-afterLen; i++) {
                numStr = numStr+"0";
            }
        }*/
        return numStr;
    }

    public static String roundByScale(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The   scale   must   be   a   positive   integer   or   zero");
        }
        if(scale == 0){
            return new DecimalFormat("0").format(v);
        }
        String formatStr = "0.";
        for(int i=0;i<scale;i++){
            formatStr = formatStr + "0";
        }
        return new DecimalFormat(formatStr).format(v);
    }

    /**
     * @author chengfy
     * created at 15/12/21 21:06
     * @description 校验条码是否正确
     * 规则：有效条码必定为13位数字，前8位数字减去66666666后为30天内的日期
     */
    public static boolean checkScanCode(String scanCode) {
        if (StringUtils.isEmpty(scanCode)) {
            return false;
        }
        if (scanCode.length() != 13) {
            return false;
        }
        try {
            String str_8 = scanCode.substring(0, 7);
            if (isNumber(str_8)) {
//                Date date = new Date(Long.valueOf(str_8) - 66666666);
                long code = Long.valueOf(str_8);
                long today = new Date().getTime();
                if (code - today > 30 * 86400000) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } catch (Exception e) {

        }
        return false;
    }

}
